//https://leetcode-cn.com/problems/word-pattern/
class Solution {
public:
    void getNextWord(string &s, int &j, string &t) {
        t = "";
        while (s[j] == ' ') ++j;
        while (s[j] && s[j] != ' ') t += s[j++];
        return ;
    }
    bool wordPattern(string p, string s) {
        string ps[128];
        unordered_map<string, char> sp;
        string t;
        int j = 0;
        for (int i = 0; p[i]; i++) {
            getNextWord(s, j, t);
            if (t == "") return false;
            if (ps[p[i]] == "") ps[p[i]] = t;
            else if (ps[p[i]] != t) return false;
            if (sp.find(t) == sp.end()) sp[t] = p[i];
            else if (sp[t] != p[i]) return false;
        }
        if (j != s.size()) return false;
        return true;
    }
};